package com.qingfeng.qfcms.controller.Admin.user;

import com.qingfeng.qfcms.controller.BaseController;
import com.qingfeng.qfcms.domin.Organization;
import com.qingfeng.qfcms.domin.User;
import com.qingfeng.qfcms.page.Page;
import com.qingfeng.qfcms.service.OrganizationService;
import com.qingfeng.qfcms.service.UserService;
import com.qingfeng.qfcms.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.*;


/**
 * 用户信息
 * Created by User on 2016/7/19.
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private OrganizationService organizationService;
    @RequestMapping("/UserList.shtml")
    public String toUserList(Model model,Page<User> page, User user,String lastLoginStart,String lastLoginEnd ){

        Map<String, Object> params=new HashMap<String, Object>();


        if (null !=lastLoginStart && !"".equals(lastLoginStart)){
            params.put("lastLoginStart",lastLoginStart);
        }
        if (null !=lastLoginEnd && !"".equals(lastLoginEnd)){
            params.put("lastLoginEnd",lastLoginEnd);
        }
        if(null != user.getUSERNAME() && !"".equals(user.getUSERNAME())){
            params.put("USERNAME", user.getUSERNAME().trim());
        }
        if(null != user.getOrg_id() && !"".equals(user.getOrg_id())){
            params.put("org_id", user.getOrg_id().trim());
        }

        page.setParams(params);
        List<User> users=userService.findUserRoleOrganizationList(page);
        List<Organization> organizations=organizationService.list(null);
        model.addAttribute("organizations",organizations);
        //String log_url=Constants.IMAGE_URL1+users.get(0).getLOGO();
        //users.get(0).setLOGO(log_url);
        model.addAttribute("users",users);
        //users.get(0).
        model.addAttribute("page",page.getPageStr("/user/UserList.shtml"));
        return "/admin/frame/user/user_list";
    }


    @RequestMapping("deleteU.shtml")
    @ResponseBody
    public boolean deleteU(String userId){
        try {
            userService.deleteById(userId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }



    }

    @RequestMapping("toEditU.shtml")
    public String toEditU(Model model,String user_id){
        User user=userService.get(user_id);
        model.addAttribute("user",user);
        return "/admin/frame/user/user_edit";

    }

    @RequestMapping("deleteUIds")
    @ResponseBody
    public boolean deleteUIds(String[] userIds){
        try {
            userService.delete(userIds);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    @RequestMapping("toAddU.shtml")
    public String toAddU(Model model){
         List<Organization> organizations=organizationService.list(null);
         model.addAttribute("organizations",organizations);
        return "/admin/frame/user/user_add";

    }

    @RequestMapping("addU.shtml")
    @ResponseBody
    public boolean addU(User user, HttpServletRequest request){
        try {
            user.setUSER_ID(uuuidUtils.toUUUID());
            user.setSTATUS("1");
            user.setPASSWORD(MD5.md5("mengxiaodong@126.com"+user.getPASSWORD()));
            user.setIP(request.getRemoteHost());
            user.setLAST_LOGIN(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
            userService.insert(user);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @RequestMapping("edit.shtml")
    @ResponseBody
    public boolean edit(User user,HttpServletRequest request){
        try {
            user.setIP(request.getRemoteAddr());
            userService.update(user);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }


    /*
	 * 导出用户信息到EXCEL
	 * @return
	 */
    @RequestMapping("excel.shtml")
    public ModelAndView exportExcel(){
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            //检索条件===
                String USERNAME = pd.getString("USERNAME");
                if(null != USERNAME && !"".equals(USERNAME)){
                    USERNAME = USERNAME.trim();
                    pd.put("USERNAME", USERNAME);
                }
                String lastLoginStart = pd.getString("lastLoginStart");
                String lastLoginEnd = pd.getString("lastLoginEnd");
                String org_id = pd.getString("org_id");
                if(lastLoginStart != null && !"".equals(lastLoginStart)){
                    lastLoginStart = lastLoginStart+" 00:00:00";
                    pd.put("lastLoginStart", lastLoginStart);
                }
                if(lastLoginEnd != null && !"".equals(lastLoginEnd)){
                    lastLoginEnd = lastLoginEnd+" 00:00:00";
                    pd.put("lastLoginEnd", lastLoginEnd);
                }
                if (org_id !=null && !"".equals(org_id)){
                    pd.put("org_id",org_id);
                }
                //检索条件===

                Map<String,Object> dataMap = new HashMap<String,Object>();
                List<String> titles = new ArrayList<String>();

                titles.add("用户名"); 		//1
                titles.add("编号");  		//2
                titles.add("姓名");			//3
                titles.add("职位");			//4
                titles.add("手机");			//5
                titles.add("邮箱");			//6
                titles.add("最近登录");		//7
                titles.add("上次登录IP");	//8

                dataMap.put("titles", titles);

                List<PageData> userList = userService.findPd(pd);
                List<PageData> varList = new ArrayList<PageData>();
                for(int i=0;i<userList.size();i++){
                    PageData vpd = new PageData();
                    vpd.put("var1", userList.get(i).getString("USERNAME"));		//1
                    vpd.put("var2", userList.get(i).getString("NUMBER"));		//2
                    vpd.put("var3", userList.get(i).getString("NAME"));			//3
                    vpd.put("var4", userList.get(i).getString("org_name"));	//4
                    vpd.put("var5", userList.get(i).getString("PHONE"));		//5
                    vpd.put("var6", userList.get(i).getString("EMAIL"));		//6
                    vpd.put("var7", userList.get(i).getString("LAST_LOGIN"));	//7
                    vpd.put("var8", userList.get(i).getString("IP"));			//8
                    varList.add(vpd);
                }
                dataMap.put("varList", varList);
                ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
                mv = new ModelAndView(erv,dataMap);

        } catch(Exception e){
            logger.error(e.toString(), e);
        }
        return mv;
    }

    @RequestMapping("reatPwd.shtml")
    @ResponseBody
    public boolean reatPwd(User user){

        try {
            user.setPASSWORD(MD5.md5("mengxiaodong@126.com"+"123456"));
            userService.updateIp(user);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    @RequestMapping("/toUserList.shtml")
    @ResponseBody
    public List<User> UserList(Page<User> page, User user,String lastLoginStart,String lastLoginEnd ){

        Map<String, Object> params=new HashMap<String, Object>();


        if (null !=lastLoginStart && !"".equals(lastLoginStart)){
            params.put("lastLoginStart",lastLoginStart);
        }
        if (null !=lastLoginEnd && !"".equals(lastLoginEnd)){
            params.put("lastLoginEnd",lastLoginEnd);
        }
        if(null != user.getUSERNAME() && !"".equals(user.getUSERNAME())){
            params.put("USERNAME", user.getUSERNAME().trim());
        }
        if(null != user.getOrg_id() && !"".equals(user.getOrg_id())){
            params.put("org_id", user.getOrg_id().trim());
        }

        page.setParams(params);
        List<User> users=userService.findUserRoleOrganizationList(page);
        return users;
    }

}
